/**
 * 棋子释放技能
 */
function chessDoSkill(gridX,gridY) {
    const chessObj = DATA_CHESS[DATA_CURRENT_SELECTED_CHESS];
    const config = chessObj.config;
    const chessGrid = chessObj.grid;
    const chessPiece = chessObj.piece;
    const skills = config.skill;
    let skill = null;
    for (let i = 0; i < skills.length; i++) {
        let skill_i = skills[i];
        if (skill_i.name === DATA_CURRENT_SKILL) {
            skill = skill_i;
            break;
        }
    }
    if (skill == null) {
        return;
    }
    let skillType = skill.type;
    switch (skillType) {
        case "SUMMON":
            chessSkillOnSummon(skill,chessGrid,chessPiece,gridX,gridY);
            break;
        case "BUFF":
            chessSkillOnBuff(skill,chessGrid,gridX,gridY);
            break;
        case "DEATH":
            chessSkillOnDeath(skill,chessGrid,gridX,gridY);
            break;
    }
}
function isValidSkillGrid(originGrid,validActions,clan,gridX,gridY) {
    const originX = parseInt(originGrid.split(",")[0]);
    const originY = parseInt(originGrid.split(",")[1]);
    //合法的目标格子
    let isValidGrid = false;
    //判断是否正确
    for (let i = 0; i < validActions.length; i++) {
        let validAction = validActions[i];
        //判断是否是可以前往的目的地
        let absolute = validAction.absolute;
        let offsetX = validAction.x;
        let targetX = originX + offsetX;
        if (offsetX > 0) {//右移
            if (gridX > targetX) {
                continue;
            }
        }
        else if (offsetX < 0) {//左移
            if (gridX < targetX) {
                continue;
            }
        }
        else {
            if (gridX !== targetX) {
                continue;
            }
        }
        if (absolute && gridX !== targetX) {
            continue;
        }
        let offsetY = validAction.y;
        let targetY;
        if (clan === 'red') {
            targetY = originY + offsetY;
            if (offsetY > 0) {//上移
                if (gridY > targetY) {
                    continue;
                }
            }
            else if (offsetY < 0) {//下移
                if (gridY < targetY) {
                    continue;
                }
            }
            else {
                if (gridY !== targetY) {
                    continue;
                }
            }
        }
        else if (clan === 'black') {
            targetY = originY - offsetY;
            if (offsetY > 0) {//上移
                if (gridY < targetY) {
                    continue;
                }
            }
            else if (offsetY < 0) {//下移
                if (gridY > targetY) {
                    continue;
                }
            }
            else {
                if (gridY !== targetY) {
                    continue;
                }
            }
        }
        if (absolute && gridY !== targetY) {
            continue;
        }
        //判断是否有阻挡
        let bx = validAction.bx;
        let by = validAction.by;
        if (bx != null && by != null) {
            let blockX = bx + originX;
            let blockY;
            if (clan === 'red') {
                blockY = originY + by;
            }
            else if (clan === 'black') {
                blockY = originY - by;
            }
            let chessBlock = DATA_CHESS_GRID[blockX+','+blockY];
            if (chessBlock != null) {
                continue;
            }
        }
        isValidGrid = true;
        break;
    }
}
function chessSkillOnSummon(skill,chessGrid,chessPiece,gridX,gridY) {
    //获取释放次数
    let checkTimesKey = DATA_CURRENT_SELECTED_CHESS+":"+DATA_CURRENT_SKILL;
    let checkTimes = DATA_SKILL_USED_TIMES[checkTimesKey];
    if (checkTimes == null) {
        checkTimes = 0;
    }
    if (checkTimes >= skill.max) {
        return;
    }
    //判断位置是否合法
    const targetGrid = gridX+','+gridY;
    let targetCheck = DATA_CHESS_GRID[targetGrid];
    if (targetCheck != null) {
        return;
    }
    const validActions = skill.targetPosition;
    const clan = chessPiece.dataset.clan;
    if (!isValidSkillGrid(chessGrid,validActions,clan,gridX,gridY)) {
        return;
    }
    //判断棋子类型的合法性
    let pieceType = skill.summonType;
    let pieceConfig = CONFIG_CHESS[pieceType];
    if (pieceConfig == null) {
        return;
    }
    //创建棋子
    var board = document.getElementById('chessboard');
    var cellSize = RULE_CELL_SIZE;
    let piece = document.createElement('div');
    piece.className = 'piece';
    piece.textContent = pieceType;
    piece.dataset.type = pieceType;
    piece.dataset.clan = clan;
    piece.dataset.x = gridX;
    piece.dataset.y = gridY;
    piece.id = `${clan}_${pieceType}_${gridX}_${gridY}_SUMMON_${DATA_CURRENT_ROUND}`;
    piece.style.left = (gridX * cellSize + (cellSize - 60) / 2) + 'px';
    piece.style.top = ((board.offsetHeight - cellSize * (gridY + 1)) + (cellSize - 60) / 2 - 4) + 'px';
    piece.style.color = clan === 'red' ? '#C00' : '#000';
    let nameLength = pieceType.length;
    if (nameLength === 1) {
        piece.style.fontSize = '36px';
    }
    else if (nameLength === 2) {
        piece.style.fontSize = '24px';
    }
    else if (nameLength === 3) {
        piece.style.fontSize = '18px';
    }
    else {
        piece.style.fontSize = '12px';
    }
    piece.style.fontWeight = 'bold';
    piece.style.textShadow = '1px 1px 1px rgba(0,0,0,0.2)';
    piece.style.backgroundColor = 'antiquewhite';
    piece.style.zIndex = '3';
    board.appendChild(piece);
    DATA_CHESS[piece.id] = {
        "id":piece.id,
        "piece":piece,
        "config":pieceConfig,
        "grid":gridX+","+gridY,
        "alive":true,
    };
    DATA_CHESS_GRID[gridX+","+gridY] = DATA_CHESS[piece.id];
    //记录释放次数
    DATA_SKILL_USED_TIMES[DATA_CURRENT_SELECTED_CHESS+":"+DATA_CURRENT_SKILL] = checkTimes + 1;
}
function chessSkillOnBuff(skill,chessGrid,gridX,gridY) {
    //获取释放次数
    let checkTimesKey = DATA_CURRENT_SELECTED_CHESS+":"+DATA_CURRENT_SKILL;
    let checkTimes = DATA_SKILL_USED_TIMES[checkTimesKey];
    if (checkTimes == null) {
        checkTimes = 0;
    }
    if (checkTimes >= skill.max) {
        return;
    }
    //判断位置是否合法
    const targetGrid = gridX+','+gridY;
    let targetCheck = DATA_CHESS_GRID[targetGrid];
    if (targetCheck == null) {
        return;
    }
    const validActions = skill.targetPosition;
    const piece = chessObj.piece;
    const clan = piece.dataset.clan;
    if (!isValidSkillGrid(chessGrid,validActions,clan,gridX,gridY)) {
        return;
    }
    //TODO:

    //记录释放次数
    DATA_SKILL_USED_TIMES[DATA_CURRENT_SELECTED_CHESS+":"+DATA_CURRENT_SKILL] = checkTimes + 1;
}
function chessSkillOnDeath(skill,chessGrid,gridX,gridY) {
    //TODO:

}
/**
 * 检查亡语
 */
function checkDeathSkillTrigger(capturedId,gridX,gridY) {
    //TODO:

}